package com.mall.order.services;

import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.constants.OrderItemConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.CancelOrderRequest;
import com.mall.order.dto.CancelOrderResponse;
import com.mall.order.OrderEndService;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Service
public class OrderEndServiceImpl implements OrderEndService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    StockMapper stockMapper;

    /**
     * 取消订单---非pipeline方式
     * @param cancelOrderRequest
     * @return
     */
    @Override
    public CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest) {
        //1.response封装
        CancelOrderResponse cancelOrderResponse = new CancelOrderResponse();
        //2.设定“订单商品详情表”的CRUD规则
        Example orderItemExample = new Example(OrderItem.class);
        Example.Criteria orderItemCriteria = orderItemExample.createCriteria();
        //3.订单&库存 对象
        Order order = new Order();
        Stock stock = new Stock();
        //4.校验
        try {
            cancelOrderRequest.requestCheck();
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(cancelOrderResponse, e);
            return cancelOrderResponse;
        }

        try {
            //5.1 根据orderId，修改商品库存
            orderItemCriteria.andEqualTo("orderId", cancelOrderRequest.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            for (OrderItem orderItem : orderItems) {
                Order order1 = orderMapper.selectByPrimaryKey(orderItem.getOrderId());
                Long itemId = orderItem.getItemId();
                System.out.println(orderItem.getItemId());
                Stock stock1 = stockMapper.selectByPrimaryKey(orderItem.getItemId());
                if (OrderConstants.ORDER_STATUS_INIT == order1.getStatus()) {
                    stock1.setLockCount(-orderItem.getNum());
                    stock1.setStockCount(orderItem.getNum().longValue());
                } else {
                    stock1.setStockCount(orderItem.getNum().longValue());
                }
                stockMapper.updateStock(stock1);
            }
            //5.2 修改订单商品锁定状态
            for (OrderItem orderItem : orderItems) {
                orderItem.setId(orderItem.getId());
                orderItem.setStatus(Integer.valueOf(OrderItemConstants.INVENTORY_FREED.getCode()));
                orderItem.setUpdateTime(new Date());
                orderItemMapper.updateByPrimaryKeySelective(orderItem);
            }

        } catch (Exception e) {
            cancelOrderResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
            cancelOrderResponse.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
            e.printStackTrace();
        }

        //5.3 修改订单状态
        order.setOrderId(cancelOrderRequest.getOrderId());
        order.setStatus(OrderConstants.ORDER_STATUS_TRANSACTION_CANCEL);
        order.setUpdateTime(new Date());
        order.setCloseTime(new Date());
        int updateRows = orderMapper.updateByPrimaryKeySelective(order);

        //6.响应
        cancelOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
        cancelOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        return cancelOrderResponse;
    }

}
